function round(number, pow) {
  var pow = pow || 1;
  if ( pow > 0 ) pow = Math.pow(10, pow);
  return Math.round(parseFloat(number) * pow) / pow;
} 

function getRuleStyle(rule, param){
  var param = param || 'opacity';
  var ss = document.styleSheets;
  var response = false;  
  for(var i=0; i<ss.length; i++){
    if ( ss[i] && ss[i].cssRules ) {
      for(var j=0; j<ss[i].cssRules.length; j++){
        if (ss[i].cssRules[j].selectorText == rule) {
          response = ss[i].cssRules[j].style[param];
          break;
        }
      }
      if ( response !== false ) {
        break;
      }
    }
  }  
  return response;  
}


(function($){

  //----------------------------------------------------------------------------
  // Datalink
  //----------------------------------------------------------------------------
  $.datalink = function(el, params){
    var base = this;
    base.$el = $(el);
    base.init = function(){
      base.$el.data('href', base.$el.attr('href'));
      base.$el.attr('href', null);
    };
    base.init();
  };
  $.fn.datalink = function(params){
    return this.each(function(){
      (new $.datalink(this, params));
    });
  };
  
  //----------------------------------------------------------------------------
  // FadeHover
  //----------------------------------------------------------------------------
  $.fn.fadeHover = function(){
    var base = this;
    base.selector = $(this).selector;    
    base.init = function() {
      base.opacity = {
        normal: round(this.css('opacity'), 2), 
        hover: round(getRuleStyle(base.selector + ':hover'),2), 
        nothover: round(getRuleStyle(base.selector + '.nothover'), 2)
      };
      base.hover(base.appears, base.disappears);
    }
    
    base.appears = function() {
      $(this).stop().animate({
        opacity: base.opacity.hover
      }, 'normal');
      base.not($(this)).stop().animate({
        opacity: base.opacity.nothover
      }, 'normal');
    }
    
    base.disappears = function() {
      base.stop().animate({
        opacity: base.opacity.normal
      }, 'normal');       
    }
    
    base.init();    
    return base;   
  };
  
  //----------------------------------------------------------------------------
  // Hoover
  //----------------------------------------------------------------------------
  $.hoover = function(el, params){
    var base = this;
    base.$el = $(el);
    base.init = function(){
      base.$el.css({position:'relative'});
      var hoover = $('<span class="hoov"></span>').appendTo(base.$el).css({
        position:'absolute', top: 0, left: 0, width: '100%', height: '100%'
      }).hide();
      
      base.$el.hover(function(){
        $(this).find('.hoov').stop().fadeIn();
      }, function() {
        $(this).find('.hoov').stop().fadeOut('fast');
      });
    };
    base.init();
  };
  $.fn.hoover = function(params){
    return this.each(function(){
      (new $.hoover(this, params));
    });
  };
  

  //----------------------------------------------------------------------------
  // Menu
  //----------------------------------------------------------------------------
  $.fn.menu = function(params){
    var base         = this;
    var originHeight = 0;
    var bigHeight    = 0; 
    base.$el         = $(base);
    base.el          = base;

    base.init = function(){
      originHeight = base.$el.innerHeight();
      bigHeight    = parseInt(getRuleStyle(base.selector + '.big', 'height'));
    
      this.links = base.$el.find('a').datalink();
      this.links.fadeHover();
      base.listen();
    };
    
    base.open = function(){
      base.$el.slideDown();
    };
    
    base.close = function(){
      base.$el.slideUp();
      base.$el.css('height', base.$el.data('originHeight'));
    };
    
    base.listen = function() { 
      base.$el.find('a').add('#logo').bind('click', function() {
        var status = base.status();
        
        switch($(this).attr('name')) {
          case 'home':
            if ( status != 'closed' ) {
              base.close();
              base.bigClose();
            }
            break;
            
          case 'bio':
          case 'contact':
            if ( status != 'bigOpened' ) base.bigOpen();
            base.select($(this));
            break;
            
          case 'evenementiel':
          case 'travaux':
          case 'ceremonies':
          case 'portraits':
            if ( status == 'bigOpened') base.bigClose();
            else if ( status != 'opened' ) base.open();
            base.select($(this));
        }
        base.$el.trigger('status', {
          item: $(this)
        });
        return false;
      });
    };
    
    base.select = function(elem) {
      var elem = $(elem);
      var elems = base.$el.find('li a.selected');
  
      if ( elems.size() > 0) {
        var diff = elem.offset().left - elems.offset().left + round(elem.outerWidth()/2,1);
        if ( diff != 0 ) {
          var span = elems.find('span');
          span.data('left', span.css('left'));
          elems.find('span').stop().animate({
            'left': diff
          }, function() { 
            elems.removeClass('selected');
            elem.addClass('selected');
            span.css('left', '50%');
          });
          return;
        }        
      }
     
      elems.removeClass('selected');
      elem.addClass('selected');     
    };
    
    base.bigOpen = function() {  
      var diff = bigHeight - 95;
      var originBottom = $('#header').css('bottom');
      base.$el.animate({
        height: bigHeight
      });
      
      $('#header').animate({
        bottom: diff
      });
    };
    
    base.bigClose = function() {
      base.$el.animate({
        height: originHeight
      });
      
      $('#header').animate({
        bottom: 5
      });
    }
    
    base.status = function() {
      if ( !base.is(':visible') ) {
        return 'closed';
      }
      else {
        return (parseInt(base.$el.css('height')) == bigHeight) ? 'bigOpened' : 'opened';        
      }
    }
       
    base.init();
    return base;
  };
  
  
  //----------------------------------------------------------------------------
  // Contents & cache
  //----------------------------------------------------------------------------
  $.fn.contents = function(params){
    var base         = this;
    var cache        = null;
    var menu         = null;
    base.$el         = $(base);
    base.el          = base;

    base.init = function(params){
      var params = $.extend({
        cache: null, 
        listen: null
      }, params);
      cache = params.cache;
      menu  = params.listen;
      
      menu.bind('status', function(event, params) {
        base.load(params.item);
      });
    };
    
    base.load = function(item) {
      var name = item.attr('name');
      var href = item.attr('href') || item.data('href');
      
      if ( !base.isCached(name) ) {
        base.post(href, function() {
          base.switchPage(name);
        });
      }
      else {
        base.switchPage(name);
      }
    }
    
    base.isCached = function(item) {
      var item = $('#' + item);
      return ( item.size() <= 0 ) ? false : item;
    }
    
    base.post = function(href, callback) { 
      $.post(href, {
        ajax: true
      }, function(data){
        var item = base.cache(data);
        base.treat(item);
        callback();
      }); 
    }
    
    base.cache = function(item) {
      var item = $(item);
      item.appendTo(cache);
      return item;
    }      
    
    base.switchPage = function(name) {       
      switch (name) {
        case 'home':
        case 'evenementiel':
        case 'travaux':
        case 'ceremonies':
        case 'portraits':
          var item = base.isCached(name);
          var oldItem = base.$el.children();
          
          if (item.attr('id') != oldItem.attr('id')) {          
            oldItem.fadeOut('fast', function(){
              base.cache($(this));
              item.appendTo(base.$el).hide().fadeIn('normal');
            });
          }
          break;
          
        case 'bio': 
        case 'contact':
          var menu2 = menu.find('.w');
          var item = base.isCached(name);
          var oldItem = menu2.find('ul').next();
                  
          if ( oldItem && oldItem.length > 0 ) {
            if (item.attr('id') != oldItem.attr('id')) {  
              oldItem.fadeOut('fast', function() {
                base.cache($(this));
                item.appendTo(menu2).hide().fadeIn('normal');
              });
            }
          }
          
          else {     
            item.appendTo(menu2).hide().fadeIn('normal');          
          }
          
          
          
          break;
      }
      
      
    }
    
    base.treat = function(item) {
      item.find('.hoover').hoover();
    }
       
    base.init(params);
    return base;
  };
})(jQuery);


  
  